<template>
  <div id="login">
    <!-- 头像区域 -->
      <div class="login_box">
        <div class="avatar_box">
          <img src="~assets/img/login/login_bg.png" alt="">
        </div>

    <!-- 登陆表单区域 -->
        <el-form ref="loginFormRef" class="login_form" :model="loginForm" :rules="loginFormRules">
          <!-- 用户名 -->
          <el-form-item class="login_form-item" prop="username">
            <el-input prefix-icon="iconfont icon-user" placeholder="用户名" v-model.lazy.trim="loginForm.username"></el-input>
          </el-form-item class="login_form-item">
          <!-- 密码 -->
          <el-form-item  class="login_form-item" prop="password">
            <el-input v-model.lazy.trim="loginForm.password" prefix-icon="iconfont icon-ico-pwd1" placeholder="密码" show-password></el-input>
          </el-form-item>
          <!-- 验证码 -->
          <el-form-item class="login_form-item" prop="vcode">
            <el-input v-model="loginForm.vcode" prefix-icon="iconfont icon-codev" placeholder="验证码" @keyup.enter.native="btnLogin"></el-input>
          </el-form-item>
          <!-- 提交按钮 -->
          <el-form-item>
            <el-button type="primary" class="btn" @click="btnLogin">登 录</el-button>
          </el-form-item>
        </el-form>
      </div>
  </div>
</template>

<script>
import { login } from 'network/login'
export default {
  data(){
    return{
      //登录参数对象
      loginForm:{
        username:'guest',
        password:'123456',
        vcode:''
      },
      //表单规则验证对象
      loginFormRules:{
        //用户名验证
        username:[
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        //用户密码验证
        password:[
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ],
        //验证验证
        vcode:[
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { min: 6, max: 6, message: '验证码为6个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    //登录
    btnLogin(){
      //发起请求前对表预验证
      this.$refs.loginFormRef.validate(async valid =>{
        //验证失败直接返回，不发送请求
        if(!valid) return

        //验证成功发送请求
        const {status,message} = await login(this.loginForm)
        if(status == 'y'){
          //登录成功
          this.$message.success(message)
          //保存token到sessionStorage中
          window.sessionStorage.setItem('token','1111111111111111')
          this.$router.push('/home')
        }else{
          //登录失败
          this.$message.error(message)
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
  #login {
    background-color: #2b4b6b;
    width: 100%;
    height: 100%;

    .login_box {
      width: 450px;
      height: 300px;
      background-color: #fff;
      border-radius: 5px;
      position:absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%,-50%);

      .avatar_box {
        width: 130px;
        height: 130px;
        padding: 10px;
        border: 1px solid #eee;
        border-radius: 50%;
        box-shadow: 0 0 10px #ddd;
        position:absolute;
        left: 50%;
        transform: translate(-50%,-51%);
        background-color: #fff;

        img {
          width: 100%;
          height: 100%;
          border-radius: 50%;
          background-color: #eee;
        }
      }
      .login_form {
        position: absolute;
        bottom: 0;
        width: 100%;
        padding: 0 60px;
        box-sizing:border-box;
        .login_form-item {
          margin-bottom: 18px;
        }
        .btn {
          width: 100%;
        }
      }
    }
  }
</style>